<template>
  <div class="financial-item">
    <img class="bg" src="static/images/exchange-bg.jpg">
    <div class="financial-table">
      <financial-table :dataList="currData" itemIndex="3"></financial-table>
    </div>
  </div>
</template>
<script>
import FinancialTable from './components/FinancialTable'
import {httpHost} from '@/assets/requestData.js'
import axios from 'axios'
import $ from 'jquery'
export default {
  name: 'ExchangePage',
  components: {
    FinancialTable
  },
  data () {
    return {
      index: -1,
      getDataTimer: null,
      writeDateTimer: null,
      tempDataList: null,
      firstVisit: true,
      dataList: [],
      indexTimer: null,
      pageIndex: 1
    }
  },
  computed: {
    pageTotal () {
      if (this.dataList.length === 0) {
        return 0
      }
      if (this.dataList.length % 8 > 0) {
        return parseInt(this.dataList.length / 8) + 1
      } else {
        return parseInt(this.dataList.length / 8)
      }
    },
    currData () {
      if (this.pageIndex < this.pageTotal) {
        return this.dataList.slice((this.pageIndex - 1) * 8, this.pageIndex * 8)
      } else if (this.pageIndex === this.pageTotal) {
        return this.dataList.slice((this.pageIndex - 1) * 8, this.dataList.length)
      } else {
        return []
      }
    }
  },
  methods: {
    getFunincialData () {
      // axios.get('api/financialData.json').then(this.getResSucc)
      axios.post(httpHost + '/financial/getRealtimeFormDataByGroup' + '?sceneId=' + this.sceneId).then(this.getResSucc)
      if (this.firstVisit) {
        var this_ = this
        setTimeout(function () {
          this_.getResDataFromC()
          this_.firstVisit = false
          this_.addIndex()
        }, 5000)
      }
    },
    getResSucc (res) {
      // save to C client  存数据到c端
      if (res.data) {
        var data = res.data
        if (data.success === true) {
          try {
            window.external.WriteFile('exchangeData', JSON.stringify(data))
          } catch (error) {
            window.external.WriteLog('eastScreen getResSucc error:' + error)
          }

          // this.tempDataList = data.exChange
        }
      }
    },

    evil (fn) {
      var Fn = Function
      return new Fn('return ' + fn)()
    },
    getResDataFromC () {
      var result = this.evil(window.external.GetJsonContextWithJsonName('exchangeData'))
      this.tempDataList = result.exChange
    },
    getCData () {
      if (this.getDataTimer) {
        clearTimeout(this.getDataTimer)
      }
      var this_ = this
      this.getDataTimer = setTimeout(function () {
        // 继续定时启动
        this_.getCData()
        // 取得数据
        try {
          this_.getResDataFromC()
        } catch (error) {
          window.external.WriteLog('eastScreen getCData error:' + error)
        }
      }, 60000)
    },
    saveCData () {
      if (this.writeDateTimer) {
        clearTimeout(this.writeDateTimer)
      }
      var this_ = this
      this.writeDateTimer = setTimeout(function () {
        this_.saveCData()
        try {
          this_.getFunincialData()
        } catch (error) {
          window.external.WriteLog('eastScreen saveCData error:' + error)
        }
      }, 50000)
    },
    getSceneId () {
      let this_ = this
      var url = '../GGWisdom/GGIBMS/Json/' + this.$route.params.jsonName
      $.ajax({
        // url: '../GGWisdom/GGIBMS/Json/1532310126840_12.json',
        url: url,
        type: 'GET',
        dataType: 'JSON',
        success (data) {
          this_.sceneId = data.scenes[0].id
        },
        error (error) {
          window.external.WriteLog('eastScreen getSceneId error:' + error)
        }
      })
    },
    addIndex () {
      console.log(this.pageIndex)
      if (this.indexTimer) {
        clearTimeout(this.indexTimer)
      }
      let this_ = this
      this.indexTimer = setTimeout(function () {
        try {
          if (this_.pageIndex < this_.pageTotal) {
            this_.pageIndex++
          } else {
            clearTimeout(this.indexTimer)
            this_.dataList = this_.tempDataList
            this_.pageIndex = 1
          }
        } catch (error) {
          window.external.WriteLog('eastScreen exchange changeIndex error:' + error)
        }
        try {
          this_.addIndex()
        } catch (error) {
          var this__ = this
          this__.addIndex()
          window.external.WriteLog('eastScreen exchange addIndex error:' + error)
        }
      }, 4000)
    }
  },
  mounted () {
    try {
      this.getResDataFromC()
      this.firstVisit = false
      this.addIndex()
    } catch (error) {
      window.external.WriteLog('first get data error:' + error)
    }
    this.sceneId = this.$route.params.sceneId
    var this_ = this
    setTimeout(function () {
      this_.getFunincialData()
    }, 2000)
    this.saveCData()
    this.getCData()
    // this.addIndex()
  }
}
</script>
<style lang="stylus" scoped>
  .financial-item
    height: 100%
  .bg
    position: absolute
    left: 0
    right: 0
    top: 0
    bottom: 0
    width: 100%
    height: 100%
  .financial-table
    position: absolute
    left: 0
    right: 0
    top: 0
    bottom: 0
    width: 100%
    height: 100%
</style>
